package net.huashitong.ssydt.security.utils;


import net.huashitong.ssydt.provider.utils.StringUtils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5加密工具类
 *
 * @author bangwei.yang
 * @version 1.0
 */
public class MD5 {

    /**
     * 对数据进行MD5加密
     *
     * @param data 明文
     * @return
     */
    public static String encrypt(String data) {
        // 返回结果
        String result = "";
        try {

            MessageDigest md = MessageDigest.getInstance("MD5");
            md.reset();
            md.update(data.getBytes("UTF-8"));
            byte[] array = md.digest();

            StringBuffer md5StrBuff = new StringBuffer();
            for (int i = 0; i < array.length; i++) {
                if (Integer.toHexString(0xFF & array[i]).length() == 1) {
                    md5StrBuff.append("0").append(
                            Integer.toHexString(0xFF & array[i]));
                } else {
                    md5StrBuff.append(Integer.toHexString(0xFF & array[i]));
                }
            }
            // 16位加密，从第9位到25位
            result = md5StrBuff.substring(8, 24).toString().toUpperCase();
        } catch (Exception e) {
        }

        return StringUtils.isEmpty(result) ? data : result;
    }



    /**
     * 对数据进行MD5加密
     *
     * @param data 明文
     * @return
     */
    public static String md5(String data) {
        byte[] hash;
        try {
            hash = MessageDigest.getInstance("MD5").digest(
                    data.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Huh, MD5 should be supported?", e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Huh, UTF-8 should be supported?", e);
        }

        StringBuilder hex = new StringBuilder(hash.length * 2);
        for (byte b : hash) {
            if ((b & 0xFF) < 0x10)
                hex.append("0");
            hex.append(Integer.toHexString(b & 0xFF));
        }

        return hex.toString().toLowerCase();
    }

    public final static String getMessageDigest(byte[] buffer) {
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        try {
            MessageDigest mdTemp = MessageDigest.getInstance("MD5");
            mdTemp.update(buffer);
            byte[] md = mdTemp.digest();
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            return null;
        }
    }
}